package com.skp.shaphan.ui.buttons;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import com.skp.job.ProjectJobException;
import com.skp.shaphan.ui.dialogs.DevOptionsDialog;

public class DevOptionsOkButton extends JButton implements ActionListener, AncestorListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5881384355538806782L;
	private DevOptionsDialog dialog;
	
	public DevOptionsOkButton(DevOptionsDialog dialog) {
		super("Ok");
		this.dialog = dialog;
		this.addActionListener(this);
		this.addAncestorListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			dialog.saveToDb();
		} catch (SQLException e1) {
			ProjectJobException.displayException(e1);
			return;
		}
		dialog.dispose();
	}

	@Override
	public void ancestorAdded(AncestorEvent event) {
		this.getRootPane().setDefaultButton(this);
	}

	@Override
	public void ancestorMoved(AncestorEvent event) {
	}

	@Override
	public void ancestorRemoved(AncestorEvent event) {
	}
}
